﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using System.Data;
using LibraryO2S;
using System.Web.UI.HtmlControls;
using BusinessObjects;
using BusinessObjects.BO_GetObjects;
using System.IO;

/// <summary>
/// Summary description for Pages_Base
/// </summary>
public class Pages_Base : System.Web.UI.UserControl
{
    //Field
    private DataTable objDT;
    private int page_size;
    public int Page_size
    {
        get { return page_size; }
        set { page_size = value; }
    }
    private int cur_page = 0;
    public int Cur_page
    {
        get
        {
            if (Request["cur_page"] != null)
                cur_page = Convert.ToInt32(Request["cur_page"]);
            if (cur_page == 0)
                cur_page = 1;
            return cur_page; ;
        }
        set { cur_page = value; }
    }
    public Pages_Base()
    {
        page_size = 15;
    }
    //Connection
    protected void OpenConnection() { }
    protected void CloseConnection() { }
    //Repeater
    protected bool bindRepeater(Repeater rpt, DataTable objDT, HtmlGenericControl messages)
    {
        try
        {
            if (objDT != null && objDT.Rows.Count > 0)
            {
                rpt.Visible = true;
                rpt.DataSource = objDT;
                rpt.DataBind();
                return true;
            }
            else
            {
                rpt.Visible = false;
                rpt.DataSource = null;
                rpt.DataBind();
                messages.InnerHtml = O2S_Message.Warning("Cảnh Báo", "Chưa Có dữ liệu trong mục này!");
                return false;
            }
        }
        catch (Exception ex)
        {
            return false;
            throw ex;
        }
    }
    protected bool bindRepeater(Repeater rpt, DataTable objDT, HtmlGenericControl pager, HtmlGenericControl messages)
    {
        try
        {
            if (objDT != null && objDT.Rows.Count > 0)
            {
                /////////           Pager       //////////                                
                string url = Request.Url.ToString();
                if (url.LastIndexOf("&cur_page") > 0)
                    url = url.Substring(0, url.LastIndexOf("&cur_page")) + "&cur_page=$CUR_PAGE";
                else
                    url = url + "&cur_page=$CUR_PAGE";
                pager.InnerHtml = O2S_Pager.Paging(page_size, Cur_page, Convert.ToInt32(objDT.Rows[0]["row_count"]), url);
                pager.Visible = true;
                /////////           End Pager       ////////                                
                rpt.DataSource = objDT;
                rpt.DataBind();
                rpt.Visible = true;
                return true;
            }
            else
            {
                rpt.Visible = false;
                pager.InnerHtml = string.Empty;
                pager.Visible = false;
                rpt.DataSource = null;
                rpt.DataBind();
                messages.InnerHtml = O2S_Message.Warning("Cảnh Báo", "Chưa Có dữ liệu trong mục này!");
                return false;
            }
        }
        catch (Exception ex)
        {
            return false;
            throw ex;
        }
    }
    protected bool bindRepeater(Repeater rpt, DataTable objDT, HtmlGenericControl pager, HtmlGenericControl messages, HtmlGenericControl result)
    {
        try
        {
            if (objDT != null && objDT.Rows.Count > 0)
            {
                /////////           Pager       //////////                
                if (page_size > Convert.ToInt32(objDT.Rows[0]["row_count"]))
                    result.InnerHtml = "Bạn đang xem kết quả từ " + 1 + " đến " + objDT.Rows[0]["row_count"].ToString() + " trong tổng số " + objDT.Rows[0]["row_count"].ToString() + " kết quả";
                else
                    if (Cur_page == 1)
                        result.InnerHtml = "Bạn đang xem kết quả từ " + 1 + " đến " + Page_size + " trong tổng số " + objDT.Rows[0]["row_count"].ToString() + " kết quả";
                    else
                        result.InnerHtml = "Bạn đang xem kết quả từ " + ((Cur_page - 1) * Page_size + 1) + " đến " + Page_size * Cur_page + " trong tổng số " + objDT.Rows[0]["row_count"].ToString() + " kết quả";
                string url = Request.Url.ToString();
                if (url.LastIndexOf("&cur_page") > 0)
                    url = url.Substring(0, url.LastIndexOf("&cur_page")) + "&cur_page=$CUR_PAGE";
                else
                    url = url + "&cur_page=$CUR_PAGE";
                pager.InnerHtml = O2S_Pager.Paging(page_size, Cur_page, Convert.ToInt32(objDT.Rows[0]["row_count"]), url);
                pager.Visible = true;
                /////////           End Pager       ////////                                
                rpt.DataSource = objDT;
                rpt.DataBind();
                rpt.Visible = true;
                return true;
            }
            else
            {
                rpt.Visible = false;
                pager.InnerHtml = string.Empty;
                pager.Visible = false;
                rpt.DataSource = null;
                rpt.DataBind();
                messages.InnerHtml = O2S_Message.Warning("Cảnh Báo", "Chưa Có dữ liệu trong mục này!");
                return false;
            }
        }
        catch (Exception ex)
        {
            return false;
            throw ex;
        }
    }
    //Other
    protected void removeHGC(HtmlGenericControl htmlgenericcontrol)
    {
        if (htmlgenericcontrol.InnerHtml != String.Empty)
            htmlgenericcontrol.InnerHtml = String.Empty;
    }
    protected void search(string keyword)
    {
        if (keyword != null && !keyword.Equals("") && !keyword.Equals("Hãy nhập từ khóa !"))
        {
            string url = removeRequest();
            Response.Redirect(url + "&k_w=" + keyword);
        }
    }
    //Request
    protected string removeRequest()
    {
        string url = Request.Url.ToString();
        int conti;
        if (Request["k_w"] != null)
        {
            conti = url.LastIndexOf("k_w=");
            string start = url.Substring(0, url.LastIndexOf("&k_w="));
            string end = url.Substring(conti, url.Length - conti);
            conti = end.IndexOf("&");
            if (conti > -1)
                url = start + end.Substring(conti, end.Length - conti);
            else
                url = start;
        }
        if (Request["cur_page"] != null)
        {
            conti = url.LastIndexOf("cur_page=");
            string start = url.Substring(0, url.LastIndexOf("&cur_page="));
            string end = url.Substring(conti, url.Length - conti);
            conti = end.IndexOf("&");
            if (conti > -1)
                url = start + end.Substring(conti, end.Length - conti);
            else
                url = start;
        }
        return url;
    }
    protected string getRequest(string req, string nullval)
    {
        string val;
        if (Request[req] != null)
            val = Request[req].ToString();
        else
            val = nullval;
        return val;
    }
    protected int getRequest(string req, int nullval)
    {
        int val;
        if (Request[req] != null)
            val = Convert.ToInt32(Request[req]);
        else
            val = nullval;
        return val;
    }
    //Category
    protected void loadCategories(DropDownList ddlCategories, int c_id)
    {
        try
        {
            DataTable categoryDT = new DataTable();
            categoryDT = BO_Categories.SelectParent(c_id, 1);
            if (categoryDT.Rows.Count > 0)
            {
                foreach (DataRow drow in categoryDT.Rows)
                {
                    ddlCategories.Items.Add(new ListItem(drow["FULL_CATEGORY_NAME"].ToString(), drow["CATEGORY_ID"].ToString()));
                    loadChildCategories(ddlCategories, Convert.ToInt32(drow["CATEGORY_ID"]), "");
                }
            }
            else ddlCategories.Items.Add(new ListItem("---Chưa có danh mục nào---", c_id.ToString()));
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    private void loadChildCategories(DropDownList ddlCategories, int c_c_id, string ident)
    {
        DataTable c_categoryDT = BO_Categories.SelectParent(c_c_id, 1);
        if (c_categoryDT.Rows.Count > 0)
        {
            ident += "--- ";
            foreach (DataRow drow in c_categoryDT.Rows)
            {
                ddlCategories.Items.Add(new ListItem(ident + drow["FULL_CATEGORY_NAME"].ToString(), drow["CATEGORY_ID"].ToString()));
                loadChildCategories(ddlCategories, Convert.ToInt32(drow["CATEGORY_ID"]), ident);
            }
        }
    }
    //DropDownList
    protected void bindDDL(DropDownList ddl, DataTable objDT, string text, string value)
    {
        if (objDT != null && objDT.Rows.Count > 0)
        {
            ddl.DataSource = objDT;
            ddl.DataTextField = text;
            ddl.DataValueField = value;
            ddl.DataBind();
        }
        else
            if (ddl.Items.Count == 0)
                ddl.Items.Add(new ListItem("----------", "-1"));
    }
    protected void bindDDL(DropDownList ddl, DataTable objDT, string text, string value, string nullText, string nullValue)
    {
        if (objDT != null && objDT.Rows.Count > 0)
        {
            ddl.DataSource = objDT;
            ddl.DataTextField = text;
            ddl.DataValueField = value;
            ddl.DataBind();
        }
        else
            if (ddl.Items.Count == 0)
                ddl.Items.Add(new ListItem(nullText, nullValue));
    }
    protected void ddlSelectedIndexChanged(string req, DropDownList ddl)
    {
        string url = removeRequest();
        if (Request[req] != null)
        {
            int conti = url.LastIndexOf(req + "=");
            string start = url.Substring(0, url.LastIndexOf("&" + req + "="));
            string end = url.Substring(conti, url.Length - conti);
            conti = end.IndexOf("&");
            if (conti > -1)
                url = start + end.Substring(conti, end.Length - conti);
            else
                url = start;
        }
        Response.Redirect(url + "&" + req + "=" + ddl.SelectedValue);
    }
    //ListBox
    protected void bindLSB(ListBox lsb, DataTable objDT, string text, string value)
    {
        if (objDT != null && objDT.Rows.Count > 0)
        {
            lsb.DataSource = objDT;
            lsb.DataTextField = text;
            lsb.DataValueField = value;
            lsb.DataBind();
        }
    }
    //Module
    protected void loadModuleByType(ListBox lsb, int type_id)
    {
        objDT = BO_Module.SelectByType(type_id);
        if (objDT != null && objDT.Rows.Count > 0)
            foreach (DataRow dr in objDT.Rows)
                lsb.Items.Add(new ListItem(dr["FULL_NAME"].ToString(), dr["MODULE_ID"].ToString()));
    }
    protected void loadModuleByType(DropDownList ddl, int type_id)
    {
        objDT = BO_Module.SelectByType(type_id);
        if (objDT != null && objDT.Rows.Count > 0)
            foreach (DataRow dr in objDT.Rows)
                ddl.Items.Add(new ListItem(dr["FULL_NAME"].ToString(), dr["MODULE_ID"].ToString()));
    }
    //Group
    protected void loadGroups(DropDownList ddlGroup, int parent)
    {
        try
        {
            objDT = BO_Sec_Group_Admin.SelectParent(1, parent);
            if (objDT != null && objDT.Rows.Count > 0)
            {
                foreach (DataRow dr in objDT.Rows)
                {
                    ddlGroup.Items.Add(new ListItem(dr["GROUP_NAME"].ToString(), dr["GROUP_ADMIN_ID"].ToString()));
                    loadChildGroup(ddlGroup, Convert.ToInt32(dr["GROUP_ADMIN_ID"].ToString()), "");
                }
            }
            else ddlGroup.Items.Add(new ListItem("---Chưa có nhóm nào---", "0"));
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    private void loadChildGroup(DropDownList ddlGroup, int parentid, string identify)
    {
        DataTable objDTChild = BO_Sec_Group_Admin.SelectParent(1, parentid);
        if (objDTChild != null && objDTChild.Rows.Count > 0)
        {
            identify += "--- ";
            foreach (DataRow drChild in objDTChild.Rows)
            {
                ddlGroup.Items.Add(new ListItem(identify + drChild["GROUP_NAME"].ToString(), drChild["GROUP_ADMIN_ID"].ToString()));
                loadChildGroup(ddlGroup, Convert.ToInt32(drChild["GROUP_ADMIN_ID"].ToString()), identify);
            }
        }
    }
    protected void bindAdminModules(ListBox lsb, int parent, string list_admin_module)
    {
        try
        {
            objDT = BO_Sec_Admin_Modules.SelectParent(1, parent);
            if (objDT != null && objDT.Rows.Count > 0)
            {
                foreach (DataRow dr in objDT.Rows)
                {
                    string id = dr["ADMIN_MODULE_ID"].ToString();
                    if (!list_admin_module.Contains(";" + id + ";"))
                        lsb.Items.Add(new ListItem(dr["DESCRIPTION"].ToString(), id));
                    bindAdminModuleChild(lsb, Convert.ToInt32(id), "", list_admin_module);
                }
            }
            else
                lsb.Items.Add(new ListItem("Không tìm thấy quyền phù hợp.", "0"));
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    private void bindAdminModuleChild(ListBox lsbChild, int c_parent, string ident, string list_admin_module)
    {
        DataTable c_admin = BO_Sec_Admin_Modules.SelectParent(1, c_parent);
        if (c_admin != null && c_admin.Rows.Count > 0)
        {
            ident += "--- ";
            foreach (DataRow drChild in c_admin.Rows)
            {
                string c_id = drChild["ADMIN_MODULE_ID"].ToString();
                if (!list_admin_module.Contains(";" + c_id + ";"))
                    lsbChild.Items.Add(new ListItem(ident + drChild["DESCRIPTION"].ToString(), c_id));
                bindAdminModuleChild(lsbChild, Convert.ToInt32(c_id), ident, list_admin_module);
            }
        }
    }
    //xml
    protected bool writeAds(int mid)
    {
        try
        {
            string path = Server.MapPath("~/upload/xml/ads/" + mid.ToString() + ".xml");
            objDT = BO_Ads_Picture.SelectByModule(mid);
            objDT.TableName = mid.ToString();
            DataSet objDS = new DataSet("ads" + mid.ToString());
            objDS.Tables.Add(objDT);
            objDS.WriteXml(path);
            return true;
        }
        catch { return false; }
    }
    protected bool writeVote(DataSet objDS, bool isNew)
    {
        try
        {
            string path = Server.MapPath("~/upload/xml/vote.xml");
            if (isNew)
            {
                string oldpath = path;
                FileInfo fi = new FileInfo(oldpath);
                int i = 0;
                while (fi.Exists)
                {
                    i++;
                    oldpath += i.ToString();
                    fi = new FileInfo(oldpath);
                }
                if (i > 0)
                    File.Move(path, oldpath);
            }
            objDS.WriteXml(path);
            return true;
        }
        catch { return false; }
    }
    //regions need
    protected void binddRegion(DropDownList ddlRegion)
    {
        objDT = BO_Regions.SearchByParent(0);
        if (objDT != null && objDT.Rows.Count > 0)
        {
            DataTable childDT;
            foreach (DataRow row in objDT.Rows)
            {
                childDT = BO_Regions.SearchByParent(Convert.ToInt32(row["REGION_ID"]));
                if (childDT != null && childDT.Rows.Count > 0)
                {
                    foreach (DataRow crow in childDT.Rows)
                        ddlRegion.Items.Add(new ListItem(crow["REGION_NAME_VN"].ToString(), crow["REGION_ID"].ToString()));
                }
            }
        }
        if (ddlRegion.Items.Count < 1)
            ddlRegion.Items.Add(new ListItem("Chưa có địa điểm nào", "-1"));
    }
    protected void binddRegion(DropDownList ddlRegion, int parent)
    {
        objDT = BO_Regions.SearchByParent(parent);
        if (objDT != null && objDT.Rows.Count > 0)
        {
            DataTable childDT;
            foreach (DataRow row in objDT.Rows)
            {
                ddlRegion.Items.Add(new ListItem(row["REGION_NAME_VN"].ToString(), row["REGION_ID"].ToString()));
                childDT = BO_Regions.SearchByParent(Convert.ToInt32(row["REGION_ID"]));
                if (childDT != null && childDT.Rows.Count > 0)
                {
                    foreach (DataRow crow in childDT.Rows)
                        ddlRegion.Items.Add(new ListItem("-----" + crow["REGION_NAME_VN"].ToString(), crow["REGION_ID"].ToString()));
                }
            }
        }
    }
    protected void binddNeed(DropDownList ddlNeed, int category_id)
    {
        objDT = BO_Needs.SelectCategory(category_id, 1, 50, 1);
        if (objDT != null && objDT.Rows.Count > 0)
            foreach (DataRow row in objDT.Rows)
                ddlNeed.Items.Add(new ListItem(row["NEED_NAME"].ToString(), row["NEED_ID"].ToString()));
    }
}